﻿using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using DAL;
using BUS;


namespace WEBSITE_BANDEAL.Admin
{
    public partial class Baiviet_Insert : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lit_title.Text = "Quản lý bài viết";
            if (!IsPostBack)
                LoadData();

        }

        List<LOAI_BAI_VIET> categoryWithParents = new List<LOAI_BAI_VIET>();
        protected void load_categories(List<LOAI_BAI_VIET> list, int ma, string kytu)
        {
            foreach (LOAI_BAI_VIET cat in list)
            {
                if (ma == cat.Maloaicha)
                {
                    cat.Tenloai = "|" + kytu + cat.Tenloai;
                    categoryWithParents.Add(cat);
                    load_categories(list, cat.Maloai, kytu + kytu); //load that parent
                }
            }
        }

        private void LoadData()
        {

            Loaibaivietcolection list = LOAI_BAI_VIETBUS.GetAll();
            load_categories(list, 0, "-");
            MA_LOAI_BV.DataSource = categoryWithParents;
            MA_LOAI_BV.DataTextField = "Tenloai";
            MA_LOAI_BV.DataValueField = "Maloai";
            MA_LOAI_BV.DataBind();
            MA_LOAI_BV.Items.Insert(0, new ListItem("----NO PARENT---", "0"));

        }

        protected void btn_Insert_Click(object sender, EventArgs e)
        {

            BAI_VIET bv = new BAI_VIET();
            bv.Tenbv = Ten.Value;
            bv.Maloaibv = int.Parse(MA_LOAI_BV.SelectedValue);
            bv.Noidung = NOI_DUNG.Text;
            bv.Trangthai = 1;

            BAI_VIETBUS.insert(bv);
            Response.Redirect("Baiviet_List.aspx");
        }
    }
}